
package net.neiquan.applibrary.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.zxc.layout.autolayout.AutoLayoutActivity;


import net.neiquan.applibrary.R;
import net.neiquan.applibrary.barlibrary.ImmersionBar;
import net.neiquan.applibrary.wight.SwipeBackActivity;
import net.neiquan.applibrary.wight.SwipeBackLayout;

import butterknife.ButterKnife;

/**
 * 作者 : dyj
 * 时间 : 2016/4/25.
 */
public abstract class BaseActivity extends SwipeBackActivity {

    private MaterialDialog dialog;

    public void setTitleTv(int id) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setText(id);
    }

    public void setTitleTv(String str) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setText(str);
    }

    public void setNextTv(String str) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    public TextView getNextTv() {
        return (TextView) findViewById(R.id.next_tv);
    }

    public void setBackGone() {
        View tv = findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }

    public void back(View view) {
        finish();
    }

    @SuppressLint("NewApi")
    public void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    public void setNextImage(int id) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    protected void setTitleOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setOnClickListener(onClick);
    }

    public void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }

    public void setNextGone() {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanced) {
        super.onCreate(savedInstanced);
        // 竖屏锁定
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        setSystemBarTint();
//        ((MyApplication) this.getApplication()).addActivity(this);
        setContentView(getRootViewId());
        ButterKnife.inject(this);
        initImmersionBar();
        setViews();
        initData();
        dialog = new MaterialDialog.Builder(this)
                .title("处理中")
                .content("请稍后")
                .progress(true, 100)
                .cancelable(false).build();
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    protected void showProgress() {
        if (dialog != null && !dialog.isShowing()) {
            if (!isDestroyed()) {
                dialog.show();
            }
        }
    }

    protected void dismissProgress() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    protected ImmersionBar mImmersionBar;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();  //在BaseActivity里销毁
    }

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        JPushInterface.onPause(this);
    }

    /**
     * 设置沉浸式
     */
    public void setSystemBarTint() {
        //只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.common_gray);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /**
     * 设置沉浸式
     */
    public void setSystemBarTint_() {
        //只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.black);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 初始化View,加载布局
     *
     * @return 布局
     */
    public abstract int getRootViewId();

    /**
     * 初始化数据
     */
    public abstract void setViews();

    /**
     * 加载网络数据
     */
    public abstract void initData();

}